package com.dorje.sys.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.druid.util.StringUtils;
import com.dorje.sys.entity.Role;
import com.dorje.sys.service.RoleService;

@Controller
@RequestMapping("/role/")
public class RoleController extends BaseController<Role> {
	
	@Autowired
	RoleService roleService;
	
	private final Logger logger = LoggerFactory.getLogger(RoleController.class);
	
	@RequestMapping("index")
	public String index(){
		return "sys/role";
	}
	
	@RequestMapping("add")
	public String add( Model model){
		model.addAttribute("role", new Role());
		return "sys/role/info";
	}
	
	@RequestMapping("edit")
	public String edit(@RequestParam("id") Integer id, Model model){
		model.addAttribute("role", roleService.findInfoById(id));
		return "sys/role/info";
	}
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public void save(@ModelAttribute Role role,HttpServletRequest request, HttpServletResponse response) throws IOException{
		role.setState(1);
		String msg = null;
		System.out.println(role);
		if(StringUtils.isEmpty(role.getName())){
			msg = "角色名称不能为空";
		}else if(role.getId() != null && role.getId()>0){
			System.out.println("edit");
			Role r = roleService.findInfoById(role.getId());
			System.out.println(r);
			if(r.getId()==null){
				msg = "信息不存在";
			}else{
				roleService.update(role);
				msg="数据更新成功";
			}
		}else{
			roleService.insert(role);
			msg="数据保存成功";
		}
		response.getWriter().write(msg);
	}
	
	
}
